#!/usr/bin/python
# -*-coding:UTF-8 -*-

# ========================
# @Time  : 2020/6/1
# @Author: Sunny
# ========================

import re
import json

from common.path_constants import COMPANY_TRAINING_YAML,OPINION_YAML
from common.yaml_packaging import YamlHandle
from common.context_packaging import ContextHandle
from common.mysql_packaging import MysqlHandle




class UserContextHandle(ContextHandle):
    """
    定义一个实现登录账户信息的参数化
    """

    @classmethod
    def rep_bessky_info(cls, data):
        """
        替换bessky用户信息
        :param data:
        :return:
        """
        admin_user_id_pattern = re.compile(eval(cls.re_config(option="bessky_user_id")))
        admin_user_name_pattern = re.compile(eval(cls.re_config(option="bessky_user_name")))
        if re.search(admin_user_id_pattern, data):
            rep_user_id = cls.user_config("bessky", "userId")
            data = re.sub(admin_user_id_pattern, str(rep_user_id), data)
        if re.search(admin_user_name_pattern, data):
            rep_user_name = cls.user_config("bessky", "userName")
            data = re.sub(admin_user_name_pattern, str(rep_user_name), data)
        return data

    @classmethod
    def rep_admin_user_info(cls, data):
        """
        替换行政专员用户信息
        :param data:
        :return:
        """
        admin_user_id_pattern = re.compile(eval(cls.re_config(option="admin_user_id")))
        admin_user_name_pattern = re.compile(eval(cls.re_config(option="admin_user_name")))
        if re.search(admin_user_id_pattern, data):
            rep_user_id = cls.user_config("admin user", "userId")
            data = re.sub(admin_user_id_pattern, str(rep_user_id), data)
        if re.search(admin_user_name_pattern, data):
            rep_user_name = cls.user_config("admin user")['username']
            data = re.sub(admin_user_name_pattern, str(rep_user_name), data)
        return data

    @classmethod
    def rep_training_user_info(cls, data):
        """
        替换培训专员用户信息
        :param data:
        :return:
        """
        training_user_id_pattern = re.compile(eval(cls.re_config(option="training_user_id")))
        training_user_name_pattern = re.compile(eval(cls.re_config(option="training_user_name")))
        if re.search(training_user_id_pattern, data):
            rep_user_id = cls.user_config("training user", "userId")
            data = re.sub(training_user_id_pattern, str(rep_user_id), data)
        if re.search(training_user_name_pattern, data):
            rep_user_name = cls.user_config("training user")['username']
            data = re.sub(training_user_name_pattern, str(rep_user_name), data)
        return data

    @classmethod
    def rep_ordinary_user_info(cls, data):
        """
        替换随机普通用户信息
        :param data:
        :return:
        """
        ordinary_user_id_pattern = re.compile(eval(cls.re_config(option="ordinary_user_id")))
        ordinary_user_name_pattern = re.compile(eval(cls.re_config(option="ordinary_user_name")))
        mysql_obj = MysqlHandle()
        while True:
            ordinary_user_info = mysql_obj.get_ordinary_emp_info()
            if ordinary_user_info.get('role_name') == "普通员工":
                if re.search(ordinary_user_id_pattern, data):
                    rep_ordinary_user_id = ordinary_user_info['user_id']
                    data = re.sub(ordinary_user_id_pattern, str(rep_ordinary_user_id), data)
                if re.search(ordinary_user_name_pattern, data):
                    rep_ordinary_user_name = ordinary_user_info['user_name']
                    data = re.sub(ordinary_user_name_pattern, str(rep_ordinary_user_name), data)
                break
        mysql_obj.close_sql()
        return data

    @classmethod
    def bessky_user_parameterize(cls, data):
        """
        bessky管理员信息参数化,用于替换头部数据
        :param data:
        :return:
        """
        if isinstance(data, dict):
            data = json.dumps(data)
        data = cls.rep_bessky_info(data)
        return data

    @classmethod
    def user_parameterize(cls, data):
        """
        行政专员信息参数化
        :param data:
        :return:
        """
        if isinstance(data, dict):
            data = json.dumps(data)
        data = cls.rep_admin_user_info(data)
        data = cls.rep_ordinary_user_info(data)
        data = cls.rep_training_user_info(data)
        return data

    @classmethod
    def rep_emp_user_info(cls,data):
        """
        替换普通员工008770的用户id和用户名
        :param data:
        :return:
        """
        emp_user_id_pattern = re.compile(eval(cls.re_config(option="emp_user_id")))
        emp_user_name_pattern = re.compile(eval(cls.re_config(option="emp_user_name")))
        if re.search(emp_user_id_pattern,data):
            emp_user_id = cls.user_config("emp user")['userid']
            data = re.sub(emp_user_id_pattern,emp_user_id,data)
        if re.search(emp_user_name_pattern,data):
            emp_user_name = cls.user_config("emp user")['username']
            data = re.sub(emp_user_name_pattern,emp_user_name,data)
        return data


    @classmethod
    def emp_user_info_parameterize(cls,data):
        """
        普通员工008770信息参数化
        :param data:
        :return:
        """
        if isinstance(data,dict):
            data = json.dumps(data)
        data = cls.rep_emp_user_info(data)
        return data

    @classmethod
    def rep_bita_supervisor_user_info(cls,data):
        """
        替换主管003471的用户id和用户名
        :param data:
        :return:
        """
        bita_supervisor_id_pattern = re.compile(eval(cls.re_config(option="bita_supervisor_id")))
        bita_supervisor_name_pattern = re.compile(eval(cls.re_config(option="bita_supervisor_name")))
        if re.search(bita_supervisor_id_pattern,data):
            emp_user_id = cls.user_config("bita_supervisor_user","userId")
            data = re.sub(bita_supervisor_id_pattern,str(emp_user_id),data)
        if re.search(bita_supervisor_name_pattern,data):
            emp_user_name = cls.user_config("bita_supervisor_user","userName")
            data = re.sub(bita_supervisor_name_pattern,str(emp_user_name),data)
        return data

    @classmethod
    def bita_supervisor_user_info_parameterize(cls,data):
        """
        主管003471用户ID和用户名参数化
        :param data:
        :return:
        """
        if isinstance(data,dict):
            data = json.dumps(data)
        data = cls.rep_bita_supervisor_user_info(data)
        return data

    @classmethod
    def bita_manager_user_info_parameterize(cls,data):
        """
        经理004532用户ID和用户名参数化
        :param data:
        :return:
        """
        bita_manager_id_pattern = re.compile(eval(cls.re_config(option="bita_manager_id")))
        bita_manager_name_pattern = re.compile(eval(cls.re_config(option="bita_manager_name")))
        if isinstance(data,dict):
            data = json.dumps(data)
        if re.search(bita_manager_id_pattern,data):
            emp_user_id = cls.user_config("bita_manager_user","userId")
            data = re.sub(bita_manager_id_pattern,str(emp_user_id),data)
        if re.search(bita_manager_name_pattern,data):
            emp_user_name = cls.user_config("bita_manager_user","userName")
            data = re.sub(bita_manager_name_pattern,str(emp_user_name),data)
        return data

    @classmethod
    def attendance_user_info_parameterize(cls,data):
        """
        考勤专员信息参数化
        :param data:
        :return:
        """
        attendance_user_id_pattern = re.compile(eval(cls.re_config(option="attendance_user_id")))
        attendance_user_name_pattern = re.compile(eval(cls.re_config(option="attendance_user_name")))
        if re.search(attendance_user_id_pattern,data):
            emp_user_id = cls.user_config("attendance_user","userId")
            data = re.sub(attendance_user_id_pattern,str(emp_user_id),data)
        if re.search(attendance_user_name_pattern,data):
            emp_user_name = cls.user_config("attendance_user","userName")
            data = re.sub(attendance_user_name_pattern,str(emp_user_name),data)
        return data




if __name__ == '__main__':

    # yaml_obj = YamlHandle(OPINION_YAML)
    # yaml_data = yaml_obj.get_data('department_approval')
    # data_before = yaml_data[2]
    # #data_before = json.dumps(data_before)
    # data_after = UserContextHandle.bita_manager_user_info_parameterize(data_before)
    # print(f"参数化前：{data_before}\n参数化后：{type(data_after)}{data_after}")


    yaml_obj = YamlHandle(COMPANY_TRAINING_YAML)
    yaml_data = yaml_obj.get_data('search_com_course')
    data_before = yaml_data[0]
    data_after = UserContextHandle.user_parameterize(data_before)
    print(f"参数化前：{data_before}\n参数化后：{data_after}")

    # yaml_obj = YamlHandle(OPINION_YAML)
    # yaml_data = yaml_obj.get_data('history_list_search')
    # data_before = yaml_data[0]
    # data_before = json.dumps(data_before)
    # data_after = UserContextHandle.rep_bessky_info(data_before)
    # print(f"参数化前：{data_before}\n参数化后：{type(data_after)}{data_after}")


